/*
 * Lenovo Group
 * Copyright (c) 2015-2016 All Rights Reserved.
 * 
 * Project Name:itsm-gateway
 * Create Time: 2016年2月16日 下午3:50:39
 */
package com.lenovo.lmrp.server.itsmgateway.test.unit;

import java.util.Date;

import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;

import com.fasterxml.jackson.core.JsonParser.Feature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.xml.XmlFactory;
import com.lenovo.lmrp.base.LowerCaseMap;
import com.lenovo.lmrp.server.itsmgateway.adapter.ms.MainWebServices;

/**
 * Mock Remedy server Web Service
 * 
 * Only check input paramter whether exist as spec. and make some failed response for convention require paramter.
 * 
 * @author wujin
 * @version v1.0
 */
@WebService(endpointInterface="com.lenovo.lmrp.server.itsmgateway.adapter.ms.MainWebServices", 
            serviceName="RemedyService",
            portName="RemedyPort",
            targetNamespace = "http://services.lenovo.com/")
@SOAPBinding(style = SOAPBinding.Style.RPC)
public class MockRemedyServer implements MainWebServices {
    private ObjectMapper mapper;
    
    public MockRemedyServer() {
        mapper = new ObjectMapper(new XmlFactory());
        mapper.configure(Feature.ALLOW_UNQUOTED_CONTROL_CHARS, true);
    }
    
    @Override
    public String validateUser(String arg0) {
        String xml = null;
        if (arg0.indexOf("mstest") >= 0)
            xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
                        + "<Info>"
                        + "<content><![CDATA[]]></content>"
                        + "<status><![CDATA[ok]]></status>"
                        + "</Info>";
        else
            xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?>"
                        + "<Info>"
                        + "<content><![CDATA[用户名不在在]]></content>"
                        + "<status><![CDATA[error]]></status>"
                        + "</Info>";
        return xml;
    }

    @SuppressWarnings("unchecked")
    @Override
    public String engineerInfo(String arg0) {
        String xml = null;
        try {
            LowerCaseMap<String> event = mapper.readValue(arg0, LowerCaseMap.class);
            
            if (event.get("chrloginname").indexOf("mstest") >= 0) {
                xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?>"
                      + "<Info>"
                      + "<content>"
                      + "<EngineerStatus><![CDATA[0]]></EngineerStatus>"
                      + "<ChrFullName><![CDATA[" + event.get("chrloginname") + "]]></ChrFullName>"
                      + "<ChrEmailAddress><![CDATA[mstest21@163.com]]></ChrEmailAddress>"
                      + "<StationID2><![CDATA[0]]></StationID2>"
                      + "<ChrMobileNumber><![CDATA[110]]></ChrMobileNumber>"
                      + "</content>"
                      + "<status><![CDATA[ok]]></status>"
                      + "</Info>";
            }
            else {
                xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?>"
                      + "<Info>"
                      + "<content><![CDATA[无数据]]></content>"
                      + "<status><![CDATA[error]]></status>"
                      + "</Info>";
            }
        }
        catch (Exception exp) {
            xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?>"
                  + "<Info>"
                  + "<content><![CDATA[false]]></content>"
                  + "<status><![CDATA[Exception]]></status>"
                  + "</Info>";
        }
        return xml;
    }

    @Override
    public String updateUserPass(String arg0) {
        String xml = null;
        if (arg0.indexOf("mstest") >= 0)
            xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
                        + "<Info>"
                        + "<content><![CDATA[]]></content>"
                        + "<status><![CDATA[ok]]></status>"
                        + "</Info>";
        else
            xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?>"
                        + "<Info>"
                        + "<content><![CDATA[用户名不在在]]></content>"
                        + "<status><![CDATA[error]]></status>"
                        + "</Info>";
        return xml;
    }

    @Override
    public String validateUserPhone(String arg0) {
        return null;
    }

    @Override
    public String ticketInfo(String arg0) {
        try {
            LowerCaseMap<String> event = mapper.readValue(arg0, LowerCaseMap.class);
            String ticketID = event.get("ticketid");
            StringBuilder sb = new StringBuilder();
            sb.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
            sb.append("<Info>");
            sb.append("<content>");
            sb.append("<ticketID><![CDATA[" + ticketID + "]]></ticketID>");
            sb.append("<gettotime><![CDATA[null]]></gettotime>");
            sb.append("<CostAll><![CDATA[100.00]]></CostAll>");
            sb.append("<Expect_WarningTimeRespond><![CDATA[null]]></Expect_WarningTimeRespond>");
            sb.append("<assigneeGroupID><![CDATA[100004202]]></assigneeGroupID>");
            sb.append("<SupplyCost><![CDATA[null]]></SupplyCost>");
            sb.append("<Expect_WarningTimeResolve><![CDATA[null]]></Expect_WarningTimeResolve>");
            sb.append("<provinceName><![CDATA[江苏省]]></provinceName>");
            sb.append("<assigneeLoginName><![CDATA[1024295950@qq.com]]></assigneeLoginName>");
            sb.append("<customerPhone><![CDATA[null]]></customerPhone>");
            sb.append("<ActualCost><![CDATA[null]]></ActualCost>");
            sb.append("<Datetime><![CDATA[null]]></Datetime>");//sb.append("<Datetime><![CDATA[100.00]]></Datetime>");
            sb.append("<reportTime><![CDATA[2016-01-27 16:02:51]]></reportTime>");
            sb.append("<ChrUserID><![CDATA[null]]></ChrUserID>");
            sb.append("<TicketOprationType><![CDATA[null]]></TicketOprationType>");
            sb.append("<GravenessGrade><![CDATA[0]]></GravenessGrade>");
            sb.append("<SurveyNote><![CDATA[工单同步测试]]></SurveyNote>");
            sb.append("<Expect_WarningTimeVisit><![CDATA[null]]></Expect_WarningTimeVisit>");
            sb.append("<ProjectID><![CDATA[110452]]></ProjectID>");
            sb.append("<ProjectName><![CDATA[联邦快递（中国）有限公司IT外包-12]]></ProjectName>");
            sb.append("<Expect_CompleteTimeVisit><![CDATA[null]]></Expect_CompleteTimeVisit>");
            sb.append("<Expect_CompleteTimeResolve><![CDATA[null]]></Expect_CompleteTimeResolve>");
            sb.append("<requestPriority><![CDATA[0]]></requestPriority>");
            sb.append("<ProjectID_Delay><![CDATA[110452]]></ProjectID_Delay>");
            sb.append("<Summary><![CDATA[工单同步测试001]]></Summary>");
            sb.append("<tickettype><![CDATA[0]]></tickettype>");
            sb.append("<DetailProject><![CDATA[联邦快递（中国）有限公司IT外包-12]]></DetailProject>");
            sb.append("<CustomerID><![CDATA[000000000123509]]></CustomerID>");
            sb.append("<Expect_CompleteTimeRespond><![CDATA[null]]></Expect_CompleteTimeRespond>");
            sb.append("<AppStatus><![CDATA[null]]></AppStatus>");
            sb.append("<submiterFullname><![CDATA[mstest25]]></submiterFullname>");
            sb.append("<PendingTime><![CDATA[null]]></PendingTime>");
            sb.append("<CityName><![CDATA[南京市]]></CityName>");
            sb.append("<chrCategory><![CDATA[5%'复印机]]></chrCategory>");
            sb.append("<Solution_Summary><![CDATA[null]]></Solution_Summary>");
            sb.append("<customerMobile><![CDATA[18511460720]]></customerMobile>");
            sb.append("<Attachment_requirements><![CDATA[1]]></Attachment_requirements>");
            sb.append("<ActualCompleteTime><![CDATA[null]]></ActualCompleteTime>");
            sb.append("<assigneeFullName><![CDATA[赵晗]]></assigneeFullName>");
            sb.append("<customerName><![CDATA[jiangren]]></customerName>");
            sb.append("<chrItem><![CDATA[调整、设置]]></chrItem>");
            sb.append("<Description><![CDATA[工单同步测试]]></Description>");
            sb.append("<chrSystem><![CDATA[常规服务]]></chrSystem>");
            sb.append("<TicketInstanceId><![CDATA[HP005056ba565eeXmoVgIPl_KQAO9Y]]></TicketInstanceId>");
            sb.append("<assigneeGroupName><![CDATA[南京APP派单组]]></assigneeGroupName>");
            sb.append("<StepName><![CDATA[服务实施]]></StepName>");
            sb.append("<RespondLevel><![CDATA[5*8*4]]></RespondLevel>");
            sb.append("<BackReasonAPP><![CDATA[null]]></BackReasonAPP>");
            sb.append("<appointment><![CDATA[null]]></appointment>");
            sb.append("<County><![CDATA[null]]></County>");
            sb.append("<chrType><![CDATA[复印机硬件故障]]></chrType>");
            sb.append("<customerAddress><![CDATA[江苏省南京市北京路10号]]></customerAddress>");
            sb.append("<ISGRAB><![CDATA[1]]></ISGRAB>");
            sb.append("<EvaluateResults><![CDATA[null]]></EvaluateResults>");
            sb.append("<pendingcause><![CDATA[null]]></pendingcause>");
            sb.append("</content>");
            sb.append("<status><![CDATA[ok]]></status>");
            sb.append("</Info>");
            
            return sb.toString();
        }
        catch (Exception exp) {
            return "<?xml version=\"1.0\" encoding=\"utf-8\"?>"
                  + "<Info>"
                  + "<content><![CDATA[false]]></content>"
                  + "<status><![CDATA[Exception]]></status>"
                  + "</Info>";
        }
    }

    @Override
    public String createTicket(String arg0) {
        String xml = null;
        xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?>"
              + "<Info>"
            + "<content><![CDATA[EV-10132-1111111-20160122-0001]]></content>"
            + "<status><![CDATA[ok]]></status>"
        + "</Info>";
        return xml;
    }

    @Override
    public String engneerStatus(String arg0) {
        String xml = null;
        if (arg0.indexOf("mstest") >= 0)
            xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
                        + "<Info>"
                        + "<content><![CDATA[]]></content>"
                        + "<status><![CDATA[ok]]></status>"
                        + "</Info>";
        else
            xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?>"
                        + "<Info>"
                        + "<content><![CDATA[用户名不在在]]></content>"
                        + "<status><![CDATA[error]]></status>"
                        + "</Info>";
        return xml;
    }

    @Override
    public String attachmentInfo(String arg0) {
        String xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?> <Info><status><![CDATA[ok]]></status> <content><![CDATA[000000000003885]]></content> </Info>";
        return xml;
    }

    @Override
    public String partsInfo(String arg0) {
        String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
                        + "<Info>"
                        + "<content><![CDATA[true]]></content>"
                        + "<status><![CDATA[ok]]></status>"
                        + "</Info>";
        return xml;
    }

    /** 
     * @see com.lenovo.lmrp.server.itsmgateway.adapter.ms.MainWebServices#updateTicket(java.lang.String)
     */
    @Override
    public String updateTicket(String arg0) {
        String xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?>"
                     + "<Info>"
                     + "<content><![CDATA[同步成功" + new Date() + "]]></content>"
                     + "<status><![CDATA[exception]]></status>"
                     + "</Info>";
        return xml;
    }

}
